# -*- encoding=utf8 -*-
__author__ = "EDY"

from airtest.core.api import *

auto_setup(__file__)
import os

# stop_app("com.abupdate.fota_demo_iot")
# start_app("com.abupdate.fota_demo_iot")

from time import sleep
import os
import subprocess


device_name = "4466505c"

def run_log_cycle():
    push_cmd = "adb push D:\monkey_log\log_new.sh /sdcard/all_logs"
    out = os.popen(push_cmd).read()
    print("日志文件已经传入颈环")
    sleep(2)
    adb_shell = subprocess.Popen("adb shell",stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
    sleep(2)
    cd_cmd = f"cd sdcard/all_logs\n"
    byte_cd_cmd = cd_cmd.encode(encoding='utf-8')
    adb_shell.stdin.write(byte_cd_cmd)
    print(f"cd sdcard/all_logs\n")
    sleep(3)
    adb_shell.stdin.flush()
    sleep(2)
    nohup_cmd = "nohup sh log_new.sh &\n"
    byte_nohup_cmd = nohup_cmd.encode(encoding='utf-8')
    adb_shell.stdin.write(byte_nohup_cmd)
    print("nohup sh log_new.sh &\n")
    sleep(2)
    adb_shell.stdin.flush()
    sleep(2)
    exit_cmd = "exit\n"
    byte_exit_cmd = exit_cmd.encode(encoding='utf-8')
    adb_shell.stdin.write(byte_exit_cmd)
    print("exit\n")
    adb_shell.stdin.flush()
#     # adb_shell.wait()
    print("success")
    

class UpdateOta:
    
    def start_update_ota(self):
        start_app("com.abupdate.fota_demo_iot")
        log("启动系统升级应用")
    
    def stop_update_ota(self):
        stop_app("com.abupdate.fota_demo_iot")
        log("关闭系统升级应用")
    
    def update_ota(self):
        run_log_cycle()
        sleep(3)
        try:
            self.start_update_ota()    
            sleep(3)
            try:
                touch(Template(r"tpl1692095075937.png", record_pos=(-0.003, -0.005), resolution=(1920, 1200)))
            except: pass
            sleep(10)
            ST.SAVE_IMAGE = False
            if exists(Template(r"tpl1692098793525.png", record_pos=(-0.04, -0.048), resolution=(1920, 1200))):
                sleep(4)
                touch(Template(r"tpl1692098831311.png", record_pos=(0.102, -0.014), resolution=(1920, 1200)))

                log("网络不好下载升级包失败")
                sleep(5)

                if exists(Template(r"tpl1692180616298.png", record_pos=(-0.002, -0.022), resolution=(1920, 1200))):
                    sleep(5)
                    touch(Template(r"tpl1692180616298.png", record_pos=(-0.002, -0.022), resolution=(1920, 1200)))
                sleep(5)
                if exists(Template(r"tpl1692188644836.png", record_pos=(-0.002, 0.013), resolution=(1920, 1200))):
                    sleep(5)
                    touch(Template(r"tpl1692188644836.png", record_pos=(-0.002, 0.013), resolution=(1920, 1200)))

                sleep(4)
            """
            已经是最新版本，无需升级
            """
            for i in range(5):


                if exists(Template(r"tpl1692180616298.png", record_pos=(-0.002, -0.022), resolution=(1920, 1200))) or exists(Template(r"tpl1692095141028.png", record_pos=(-0.106, -0.048), resolution=(1920, 1200))) or exists(Template(r"tpl1692181070074.png", record_pos=(-0.003, 0.01), resolution=(1920, 1200))):
                    log("找到下载或者提示新版本")
                    break
                sleep(5)
            try:
                if exists(Template(r"tpl1692095141028.png", record_pos=(-0.106, -0.048), resolution=(1920, 1200))):
                    log("已经是最新版本，无需升级")

                    touch(Template(r"tpl1692095186631.png", record_pos=(0.102, -0.014), resolution=(1920, 1200)))
                    sleep(2)
    #                 self.stop_update_ota()
            except:pass                                
            #需要升级
                            
            ST.SAVE_IMAGE = True

            if exists(Template(r"tpl1692180616298.png", record_pos=(-0.002, -0.022), resolution=(1920, 1200))):
                log("ota需要升级到")
                sleep(2)
                touch(Template(r"tpl1692097115299.png", record_pos=(-0.001, -0.006), resolution=(1920, 1200)))

            for i in range(140):
                ST.SAVE_IMAGE = False
                if exists(Template(r"tpl1692097675389.png", record_pos=(-0.001, -0.045), resolution=(1920, 1200))):
                    log("已经下载升级包完毕，准备升级")
                    break
                    sleep(2)
                sleep(5)
            ST.SAVE_IMAGE = True
            log("准备开始升级了~~~~~~~")
            touch(Template(r"tpl1692097675389.png", record_pos=(-0.001, -0.045), resolution=(1920, 1200)))
            """
            判断升级过程
            """
            for i in range(100):
                ST.SAVE_IMAGE = False
                if (not exists(Template(r"tpl1692183693517.png", record_pos=(-0.002, 0.01), resolution=(1920, 1200)))) or exists(Template(r"tpl1692184002814.png", record_pos=(-0.059, -0.016), resolution=(1920, 1200))):
                    log("升级过程完毕了")
                    break
                log("升级过程中~~~~~~~~~~")                
                sleep(10)
            sleep(2)
            try:
                if exists(Template(r"tpl1692097927658.png", record_pos=(-0.115, -0.053), resolution=(1920, 1200))):
                    log("升级失败了")
                    touch(Template(r"tpl1692097977769.png", record_pos=(0.101, -0.014), resolution=(1920, 1200)))
                    sleep(2)
                    self.stop_update_ota()
            except:pass
            sleep(2)
            ST.SAVE_IMAGE = True
            if exists(Template(r"tpl1692184002814.png", record_pos=(-0.059, -0.016), resolution=(1920, 1200))):
                sleep(2)
                touch(Template(r"tpl1692184053727.png", record_pos=(0.101, 0.018), resolution=(1920, 1200)))
            sleep(60*2)
            #shell(f"adb -s {device_name}  wait-for-device")
            connect_device(f"android://127.0.0.1:5037/{device_name}")
            log("*"*30,"设备重启连接成功")
            sleep(10)
        except Exception as e:
            raise e
        finally:
            stop_update_ota()
        
#         try:
#             self.push_run_log_cycle()                    
#         except:pass
        
if __name__=="__main__":
    uo = UpdateOta()
    for i in range(1,3):
        uo.update_ota()
        print(f"-------完成{i}次-------")
        sleep(5)
        
        
    
    
    
    
    
    
    
    